package com.taiyo.modules.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.taiyo.common.utils.JwtUtils;
import com.taiyo.common.utils.MessageUtils;
import com.taiyo.common.utils.R;
import com.taiyo.common.utils.RedisUtils;
import com.taiyo.common.utils.ShiroUtils;
import com.taiyo.common.validator.Assert;
import com.taiyo.modules.api.annotation.AuthIgnore;
import com.taiyo.modules.sys.entity.SysUserEntity;
import com.taiyo.modules.sys.service.SysUserService;

/**
 * 注册
 */
@RestController
@RequestMapping("/api")
public class ApiRegisterController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private JwtUtils jwtUtils;
	@Autowired
	private RedisUtils redisUtil;
	/**
	 * 获取验证码
	 */
	@AuthIgnore
	@PostMapping("/getCode")
	public R getCode(@RequestParam Map<String, Object> params) {
		System.out.println("params:" + params);
		String mobile = params.get("mobile") == null ? "" : params.get("mobile").toString();
		Assert.isBlank(mobile, "电话号码不能为空");
		
        String code = MessageUtils.getRandNum(6);
        System.out.println("code:"+code);
        //发送
        try {
        	MessageUtils.sendMessage(mobile, code);
			Map<String, Object> map = new HashMap<>();
			map.put("mobile", mobile);
			//5分钟过期
			map.put("expire", 300);
			map.put("code", code);
			redisUtil.set(mobile, code, 300);
			return R.ok().put("result", map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return R.error("验证码发送失败");
		}
		
	}
	/**
	 * 注册
	 */
	@AuthIgnore
	@PostMapping("/register")
	public R register(@RequestParam Map<String, Object> params) {
		System.out.println("params:" + params);
		String username = params.get("username") == null ? "" : params.get("username").toString();
		String password = params.get("password") == null ? "" : params.get("password").toString();
		String email = params.get("email") == null ? "" : params.get("email").toString();
		String mobile = params.get("mobile") == null ? "" : params.get("mobile").toString();
		String code = params.get("code") == null ? "" : params.get("code").toString();
		Assert.isBlank(username, "用户名不能为空");
		Assert.isBlank(mobile, "电话号码不能为空");
		Assert.isBlank(password, "密码不能为空");
		Assert.isBlank(code, "验证码不能为空");
		
		String codeRedis = redisUtil.get(mobile);
		if (null ==codeRedis || !codeRedis.equalsIgnoreCase(code)) {
			return R.error("验证码不正确");
		}
		SysUserEntity user1 = sysUserService.queryByUserName(username);
		// 账号存在
		if (user1 != null) {
			return R.error("账号已存在");
		}
		SysUserEntity user2 = sysUserService.queryByMobile(mobile);
		// 手机号存在
		if (user2 != null) {
			return R.error("手机号码已被注册");
		}

		SysUserEntity user = new SysUserEntity();
		user.setAccount(username);
		user.setPassword(password);
		user.setEmail(email);
		user.setMobile(mobile);
		user.setRoleIdList(new ArrayList());
		user.setDeptId(1L);
		user.setStatus(1);
		sysUserService.save(user);

		// 生成token
		String token = jwtUtils.generateToken(user.getUserId());

		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		map.put("expire", jwtUtils.getExpire());
		map.put("user", user);
		redisUtil.set(token, user, jwtUtils.getExpire());
		return R.ok(map);
	}
}
